package com.ruoyi.project.purchase.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.RyPurchaseOrder;
import com.ruoyi.project.purchase.domain.RyQuality;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import com.ruoyi.project.purchase.mapper.RyPurchaseOrderMapper;
import com.ruoyi.project.purchase.mapper.RyQualityMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyDepotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname infcController
 * @Description TODO
 * @Date 2022/9/23 18:04
 * @Created by You
 */

//@Anonymous
@RestController
@RequestMapping("/infcQuality")
public class InfcControllerQuality {
    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;

    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private RyDepotMapper ryDepotMapper;

    @Autowired
    private RyPurchaseOrderMapper ryPurchaseOrderMapper;


    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;

    @Autowired
    private RyQualityMapper ryQualityMapper;



    /**
     * --采购质检
     * 新增采购质检信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
//        System.out.println("depotDetailBean---->" + depotDetailBean);
        System.out.println("linkHeadId---->" + depotHeadItem.getLinkHeadId());
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //新增购质检单信息
        RyQuality ryQuality = new RyQuality();
        ryQuality.setNumber(depotHeadItem.getNumber());//单据号
        ryQuality.setFileName(depotHeadItem.getFileName());
        ryQuality.setSalesMan(depotHeadItem.getSalesMan());
        ryQuality.setCreateTime(DateUtils.getNowDate());//创建时间
        ryQuality.setCreator(user.getUserId());//操作员
        ryQuality.setRemark(depotHeadItem.getRemark());
        ryQuality.setType("其他");
        ryQuality.setStatus("0");//默认未审核
        ryQuality.setSubType("采购质检");

        //判断是否关联了采购订单
        if(depotHeadItem.getLinkHeadId() != null) {//关联了采购订单
            ryQuality.setLinkHeadId(depotHeadItem.getLinkHeadId());//关联采购订单id
            ryQuality.setLinkNumber(depotHeadItem.getNumber());
            //查询采购单信息
            RyDepotHead ryDepotHead = ryDepotHeadMapper.selectRyDepotHeadById(depotHeadItem.getLinkHeadId());
            if(ryDepotHead.getProjectHeadId() != null){//如果采购订单中存在项目总清单，则关联了项目
                ryQuality.setProjectHeadId(ryDepotHead.getProjectHeadId());
            }

        }

        int res = ryQualityMapper.insertRyQuality(ryQuality);
        if(res > 0){
            //遍历明细数据，更新单据明细数据
            for(RyDepotItem ryDepotItem : tableData){
                ryDepotItem.setHeaderId(ryQuality.getId());//设置刚生成的采购订单id
                ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
            }
        }

        return AjaxResult.success("新增成功");
    }

    /**
     * --采购订单
     * 获取所有可关联采购订单信息（采购质检使用）
     * 首先查询出质检单关联的采购订单id,这些采购订单id排除后的采购订单才是可用的
     *
     */
    @GetMapping("/allForQuality")
    public AjaxResult allForQuality(){

        RyDepotHead ryDepotHead = new RyDepotHead();
        ryDepotHead.setType("其他");
        ryDepotHead.setSubType("采购订单");
        ryDepotHead.setStatus("1");//查询所有可用采购质检,1代表“已审核”的单据
        List<RyDepotHead> list = ryDepotHeadMapper.selectRyDepotHeadList(ryDepotHead);
//        //返回给前台数据
//        List<JSONObject> resList = new ArrayList<JSONObject>();
//        for(RyDepotHead ryDepotHead1 : list){
//            JSONObject json = new JSONObject();
//            json.put("value",ryDepotHead1.getId());
//            json.put("label",ryDepotHead1.getProjectName());
//            resList.add(json);
//        }
        return AjaxResult.success(list);

//        //首先查询出所有质检单
//        RyDepotHead ryDepotHead = new RyDepotHead();
//        ryDepotHead.setType("其他");
//        ryDepotHead.setSubType("采购质检");
//        List<RyDepotHead> list = ryDepotHeadService.selectRyDepotHeadList(ryDepotHead);
//        //需要排除的id列表
//        List<Long> paramList =  new ArrayList<Long>();
//        //质检单数据不存在，查询所有
//        if(list.size() <= 0 || list == null){
//            paramList = null;
//        } else {
//            //遍历结果，筛选出关联的采购订单id
//            for(RyDepotHead ryDepotHead1 : list){
//                if(ryDepotHead1.getLinkHeadId() != null){
//                    paramList.add(ryDepotHead1.getLinkHeadId());
//                }
//            }
//            //如果没有id需要排除则赋值为null,否则数据库会报错
//            if(paramList.size() <= 0){
//                paramList = null;
//            }
//        }
//        //根据条件筛选可关联的采购订单
//        List<RyPurchaseOrder> resList = ryPurchaseOrderService.allUsed(paramList);
//        ryPurchaseOrderMapper.allUsed(list);
//        return AjaxResult.success(resList);
    }

    /**
     * 新增采购质检单时关联采购订单id查询子表信息
     */
    @GetMapping("/selectBypurchaseOrderId")
    public AjaxResult selectBypurchaseOrderId(ParamBean paramBean){
        return AjaxResult.success(ryDepotItemMapper.selectOrderItemByDepotId(paramBean.getLinkHeadId()));
    }


    /**
     * -- 采购质检
     * 获取采购订单主表及子表详细信息
     */
    @GetMapping(value = "/selectRyDepotDetailById/{id}")
    public AjaxResult selectRyDepotDetailById(@PathVariable("id") Long id)
    {

        DepotHeadItem depotHeadItem = new DepotHeadItem();
        RyQuality ryQuality = ryQualityMapper.selectRyQualityById(id);
        //主表数据
        depotHeadItem.setId(ryQuality.getId());

        depotHeadItem.setNumber(ryQuality.getNumber());//单据号

        depotHeadItem.setCreator(ryQuality.getCreator());//操作员
        depotHeadItem.setOperName(ryQuality.getOperName());//操作员名称

        depotHeadItem.setCreateTime(ryQuality.getCreateTime());
        depotHeadItem.setOperTime(ryQuality.getOperTime());
        depotHeadItem.setFileName(ryQuality.getFileName());
        depotHeadItem.setSalesMan(ryQuality.getSalesMan());

        depotHeadItem.setLinkNumber(ryQuality.getLinkNumber());//关联订单号
        depotHeadItem.setLinkHeadId(ryQuality.getLinkHeadId());//关联订单id
        depotHeadItem.setRemark(ryQuality.getRemark());//备注

        //子表数据
        List<RyDepotItem> tableData = ryDepotItemMapper.selectQualityItemByDepotId(id);
        depotHeadItem.setTableData(tableData);

        return AjaxResult.success(depotHeadItem);
    }


    /**
     *  -- 采购质检
     * 根据主表修改单据子表信息
     */
    @RequestMapping("/editQualityDetail")
    public AjaxResult editQualityDetail(@RequestBody DepotHeadItem depotHeadItem){
        System.out.println("depotDetailBean---->" + depotHeadItem);
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //质检模块只需要修改个别信息即可
        RyQuality ryQuality = new RyQuality();
        ryQuality.setId(depotHeadItem.getId());//主键，用于定位修改数据
        ryQuality.setOperTime(DateUtils.getNowDate());
        ryQuality.setFileName(depotHeadItem.getFileName());
        ryQuality.setRemark(depotHeadItem.getRemark());
        //修改
        ryQualityMapper.updateRyQuality(ryQuality);

        //将子表数据先删除，根据主表id删除
        ryDepotItemMapper.deleteDepotItemByHeadId(depotHeadItem.getId());
        //遍历明细数据，新增单据明细数据
        for(RyDepotItem ryDepotItem : tableData){
            ryDepotItem.setHeaderId(depotHeadItem.getId());//设置刚生成的采购订单id
            ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
        }

        return AjaxResult.success("修改成功");
    }
}
